const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();

    //加载一级分类全部
    router.get("/updateertiaryGoods.html",function(req,res,next){
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
        db.query("SELECT *FROM classa, classb WHERE classa.classAId = classb.classAId ORDER BY `classb`.`classBtime` DESC",function (err,data) {
            if(err)
            {
                res.status(500).send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
            }else
            {
                if(data.length==0)
                {
                    res.status(500).send('<script>alert("没有数据哦!");window.location.href="index.html";</script>').end()

                }else
                {

                    req.oneortwo=data;
                    next();
                }

            }
        })
    });

//显示详情信息
    router.get("/updateertiaryGoods.html",function(req,res,next){
        var classCId=+req.query.classCId;

        db.query("SELECT * FROM  classc,classb WHERE classc.classBId=classb.classBId and classc.classCId="+classCId+"",function(err,data){
                    if(err)
                    {
                         res.status(500).send('<script>alert("出错啦");</script>').end();
                    }else
                    {
                        if(data.length==0)
                        {
                            res.status(400).send('<script>alert("没有数据!");window.location.reload();</script>').end();
                        }else {
                           req.secondclassAId=data;
                            console.log(data)
                        }
                        next();
                    }
            })
    });


//修改
    router.use('/updateertiaryGoods',function(req,res,next){

            if(req.files.length==0)
            {

                db.query("UPDATE classc SET classBId="+ +req.body.classBId+",classCName='"+req.body.classCName+"',classCTitle='"+req.body.editorValue+"',classCprice="+ +req.body.classCprice+",classCDiscount="+ +req.body.classCDiscount+",isActive="+ +req.body.isActive+",ofBrowsing="+ +req.body.ofBrowsing+",state="+ +req.body.state+" WHERE  classCId="+ +req.body.classCId+"",function (err,data) {
                    if(err)
                    {
                        res.status(500).send('<script>alert("出错啦");</script>').end();
                    }else
                    {
                        res.status(400).send('<script>alert("修改成功");window.location.href="ertiaryGoods.html";</script>').end();
                        next();
                    }
                });

            }else {
                //console.log("原名文件"+req.files[0].originalname);
                //获得文件扩展名
                //console.log("文件后缀"+pathLib.parse(req.files[0].originalname).ext);
                //文件全名
                var ext = pathLib.parse(req.files[0].originalname).ext;
                //console.log("dxcvbnm" + ext);
                //文件旧存放路径
                var oldPath = req.files[0].path;
                // console.log("文件旧存放路径"+oldPath)
                //文件新存放路径
                var newPath = req.files[0].path + ext;
               // console.log("文件新存放路径" + newPath)
                //文件新名
                var newFileName = req.files[0].filename + ext;

                console.log("我上的图片"+newFileName);

                fs.rename(oldPath, newPath, function (err) {
                    if (err) {
                        res.status(500).send('upload is error');
                    }
                    else {
                        db.query("UPDATE classc SET classBId="+ +req.body.classBId+",classCName='"+req.body.classCName+"',classCPIc='"+newFileName+"' ,classCTitle='"+req.body.editorValue+"',classCprice="+ +req.body.classCprice+",classCDiscount="+ +req.body.classCDiscount+",isActive="+ +req.body.isActive+",ofBrowsing="+ +req.body.ofBrowsing+",state="+ +req.body.state+" WHERE  classCId="+ +req.body.classCId+"",function (err,data) {
                            if(err)
                            {
                                res.status(500).send('<script>alert("出错啦");</script>').end();
                            }else
                            {
                                res.status(400).send('<script>alert("修改成功");window.location.href="ertiaryGoods.html";</script>').end();
                                next();
                            }
                        });

                    }
                });
            }

    })


    router.get('/updateertiaryGoods.html',function(req,res){
         // res.render("admin/updateertiaryGoods.ejs",{cclassAconts:req.Namecont,secondclassAId:req.secondclassAId});
        res.render("admin/updateertiaryGoods.ejs",{secondLsit:req.oneortwo,displaylist:req.secondclassAId});
    });
    return router;
}